
% A2_import_matlab
%==============================================================================

% Description: This code places the demand estimates into a single file
% - Imports all product datasets and demand estimates into 1 file
% - Sorts products according to their price coefficients

clear
clc
KK = 3426;

cd 'D:\data_replication'

%==========================================================================

% Import Post-Demand-Estimation Data 
%==========================================================================

for k = 1:KK
    
k
    
filename_X_MPEC = sprintf('robustness/alt_iv/2_demand_estimation/3_baseline/X_MPEC_%d.csv', k);
readtable(filename_X_MPEC);
X_MPEC_summary{k} = table2array(ans(:,2:end));

filename_objective_MPEC = sprintf('robustness/alt_iv/2_demand_estimation/3_baseline/objective_MPEC_%d.csv', k);   
readtable(filename_objective_MPEC);
objective_MPEC_summary{k} = table2array(ans(:,2:end));

filename_status_MPEC = sprintf('robustness/alt_iv/2_demand_estimation/3_baseline/status_MPEC_%d.csv', k);  
readtable(filename_status_MPEC);
status_MPEC_summary{k} = table2array(ans(:,2:end));

filename_data = sprintf('robustness/alt_iv/2_demand_estimation/3_baseline/data_%d.csv', k);   
readtable(filename_data);
data_summary{k} = table2array(ans(:,2:end));

filename_Y = sprintf('robustness/alt_iv/2_demand_estimation/3_baseline/Y_save_%d.csv', k); 
readtable(filename_Y);
Y_summary{k} = table2array(ans(:,2:end));

filename_v = sprintf('robustness/alt_iv/2_demand_estimation/3_baseline/v_save_%d.csv', k); 
readtable(filename_v);
v_summary{k} = table2array(ans(:,2:end));

filename_FE = sprintf('robustness/alt_iv/2_demand_estimation/3_baseline/FE_%d.csv', k);   
readtable(filename_FE);
FE_summary{k} = table2array(ans(:,2:end));

K_data = importdata('robustness/alt_iv/2_demand_estimation/3_baseline/K.csv');
K = K_data.data;

K_A_data = importdata('robustness/alt_iv/2_demand_estimation/3_baseline/K_A.csv');
K_A = K_A_data.data;

income_mu_sigma_data = importdata('robustness/alt_iv/2_demand_estimation/3_baseline/income_mu_sigma.csv');
income_mu_sigma = income_mu_sigma_data.data;
end
clearvars filename_data filename_FE filename_objective_MPEC filename_status_MPEC filename_v filename_X_MPEC filename_Y
clearvars ans income_mu_sigma_data k K_A_data K_data

%==========================================================================

% Sort Products according to Convergence and Price Coefficient and Save
%==========================================================================

status_MPEC_vec = zeros(KK,5);
price_coeff_MPEC = zeros(KK,1);
converged = zeros(KK,1);
for k=1:KK
status_MPEC = status_MPEC_summary{k}; 
objective_MPEC = objective_MPEC_summary{k}; 
X_MPEC = X_MPEC_summary{k};
status_MPEC_index = status_MPEC~=0;
objective_MPEC_adj = objective_MPEC;
objective_MPEC_adj(status_MPEC_index) = 99999999;
[a, I] = min(objective_MPEC_adj);
X_MPEC_opt = X_MPEC(:,I);
X_MPEC_opt_summary{k} = X_MPEC_opt;
status_MPEC_vec(k,:) =  status_MPEC; 
converged(k,1) = max(status_MPEC == 0);
price_coeff_MPEC(k,:) = X_MPEC_opt(4,1); 
end
price_table = array2table([(1:KK)', converged, price_coeff_MPEC]);
[price_table_sorted, index_price_coeff] = sortrows(price_table,{'Var2','Var3'},{'descend','ascend'});
clearvars price_table_sorted converged a I status_MPEC status_MPEC_vec objective_MPEC objective_MPEC_adj

clearvars i_k k X_MPEC X_MPEC_opt
save('robustness/alt_iv/3_supply_side/main_data_supply.mat');


% Crosswalk k to i_k
%==========================================================================

%load('robustness/alt_iv/3_supply_side/main_data_supply.mat');

k_to_i_k = zeros(KK, 2);

for k = 1:KK
 
k_to_i_k(k, 1) = k;    
k_to_i_k(k, 2) = find(index_price_coeff == k);

end

filename_table = 'robustness/alt_iv/3_supply_side/k_to_i_k.csv';
data_save = array2table(k_to_i_k);
data_save.Properties.VariableNames = {'k' 'i_k'};  
writetable(data_save, filename_table, 'Delimiter',',','QuoteStrings',true);


